<template>
  <el-row :gutter="20">
    <el-col :span="3">
      <category @tree-node-click="treenodeclick"></category>
    </el-col>
    <el-col :span="21">
    <div class="mod-config">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="分类">
          <category-cascader :catelogPath.sync="categoryPath"></category-cascader>
        </el-form-item>
        <el-form-item label="价格区间">
          <el-input-number style="width:130px" v-model="dataForm.price.min" :min="0"></el-input-number> 一
          <el-input-number style="width:130px" v-model="dataForm.price.max" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="禁用状态">
          <el-select style="width:160px" v-model="dataForm.isDisable" clearable placeholder="请选择">
            <el-option label="未禁用" :value="0"></el-option>
            <el-option label="已禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检索">
          <el-input v-model="dataForm.key" placeholder="参数名" style="width: 120px" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button v-if="isAuth('product:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="isAuth('product:delete')" type="danger" @click="deleteHandle()"
                     :disabled="dataListSelections.length <= 0">批量删除
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
        @expand-change="getProductDetails">
        <el-table-column type="expand">
          <template slot-scope="scope">
            商品描述：{{scope.row.describes}}
            <br />
            商品详细描述：{{scope.row.detail}}
            <br />
            分类ID：{{scope.row.categoryId}}
            <br />
            创建时间：{{scope.row.createTime}}
            <br />
            更新时间：{{scope.row.updateTime}}
            <br />
          </template>
        </el-table-column>
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="id"
          header-align="center"
          align="center"
          label="id">
        </el-table-column>
        <el-table-column
          prop="productName"
          header-align="center"
          align="center"
          label="商品名称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="productPrice"
          header-align="center"
          align="center"
          label="商品价格">
        </el-table-column>
<!--        <el-table-column-->
<!--          prop="categoryId"-->
<!--          header-align="center"-->
<!--          align="center"-->
<!--          label="分类id">-->
<!--        </el-table-column>-->
        <el-table-column
          prop="stock"
          header-align="center"
          align="center"
          label="库存">
        </el-table-column>
<!--        <el-table-column
          prop="describes"
          header-align="center"
          align="center"
          label="商品描述"
          width="120">
        </el-table-column>
        <el-table-column
          prop="detail"
          header-align="center"
          align="center"
          label="商品详细描述"
          width="158">
        </el-table-column>-->
        <el-table-column
          prop="picture"
          header-align="center"
          align="center"
          label="商品图片"
          width="150">
          <template slot-scope="scope">
            <img :src="scope.row.picture" style="width: 100px; height: 80px"/>
          </template>
        </el-table-column>
        <el-table-column
          prop="monthSellCount"
          header-align="center"
          align="center"
          label="月销量">
        </el-table-column>
        <el-table-column
          prop="isHotSell"
          header-align="center"
          align="center"
          label="是否热销">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isHotSell"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="updateProductStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="collectionCount"
          header-align="center"
          align="center"
          label="收藏量">
        </el-table-column>
        <el-table-column
          prop="isDisable"
          header-align="center"
          align="center"
          label="是否禁用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isDisable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="updateProductStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <!--<el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          label="创建时间"
          width="95">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          header-align="center"
          align="center"
          label="更新时间"
          width="80">
        </el-table-column>-->
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
    </el-col>
  </el-row>
</template>

<script>
  import AddOrUpdate from './product-add-or-update'
  import Category from '../common/category'
  import CategoryCascader from '../common/category-cascader'
  export default {
    data () {
      return {
        catPathSub: null,
        dataForm: {
          key: '',
          categoryId: 0,
          isDisable: null,
          price: {
            min: 0,
            max: 0
          }
        },
        categoryPath: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      Category,
      CategoryCascader
    },
    //activated()：在vue对象存活的情况下，进入当前存在activated()函数的页面时，一进入页面就触发；可用于初始化页面数据等
    activated () {
      this.getDataList()
    },
    methods: {
      //树节点被点击时触发
      treenodeclick(data, node, component) {
        this.dataForm.categoryId = data.id;
        this.getDataList(); //重新查询
      },
      getProductDetails(){

      },
      //获取全部数据
      getAllDataList(){
        this.dataForm.categoryId = 0;
        this.getDataList();
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornApi('/product/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'categoryId': this.dataForm.categoryId,
            'isDisable': this.dataForm.isDisable,
            'min': this.dataForm.price.min,
            'max': this.dataForm.price.max
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 修改商品状态
      updateProductStatus (data) {
        console.log('最新状态', data)
        let {id, isDisable, isHotSell} = data
        this.$http({
          url: this.$http.adornApi('/product/update'),
          method: 'post',
          data: this.$http.adornData({id, isDisable, isHotSell}, false)
        }).then((data) => {
          console.log(data)
          if (data && data.data.code === 0) {
            this.$message({
              type: 'success',
              message: '修改状态成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: '修改状态失败'
            })
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornApi('/product/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    },
    mounted() {
      this.catPathSub = PubSub.subscribe("categoryPath", (msg, val) => {
        this.dataForm.categoryId = val[val.length - 1];
      });
    },
    beforeDestroy() {
      PubSub.unsubscribe(this.catPathSub);
    } //生命周期 - 销毁之前
  }
</script>
